package com.sq.partner.manager.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.dao.ActirationCardPowerDao;
import com.sq.partner.manager.mybatis.model.GatewayRole;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.service.ActirationCardPowerService;
import com.sq.partner.manager.util.Page;

@Service
public class ActirationCardPowerServiceImpl implements ActirationCardPowerService {

	@Resource
	private ActirationCardPowerDao powerDao;
	@Override
	public List<GatewayRole> queryUserRole(List<String> roleName) throws Exception{
		//拼接串
		StringBuffer rolenames=new StringBuffer();
		for(String name:roleName){
			rolenames.append("'"+name+"',");
		}
		//去除最后一个,
		rolenames.deleteCharAt(rolenames.length() - 1);
		return powerDao.queryroles(rolenames.toString());
	}
	@Override
	public boolean delUserRole(HashMap<String,Object> map)throws Exception{
		return powerDao.delRoles(map);
	}
	@Override
	public boolean removeUser(GatewayUser user,GatewayUser operuser) throws Exception{
		boolean flag=true;
		//重置为空
		user.setUserComcode("");
		user.setUserPower("");
		//重置归属机构及权限信息
		if(updateUserComPower(user)){
			HashMap<String,Object> map=new HashMap<String, Object>();
			map.put("userid", user.getUserid());
			map.put("flag", Constants.ACTIR_CARD_ROLE+"%");
			//删除激活卡相关角色信息
			flag=delUserRole(map);
		}else{
			flag=false;
		}
		return flag;
	}
	@Override
	public boolean updateUserComPower(GatewayUser user) throws Exception{
		return powerDao.updateUserComPower(user);
	}
	@Override
	public Page<GatewayUser> queryUser(Page<GatewayUser> page) throws Exception{
		return powerDao.queryUser(page);
	}
	@Override
	public List<GatewayRole> queryAllRole(String para) throws Exception{
		return powerDao.queryAllRole(para);
	}
	@Override
	public List<GatewayRole> queryUserRoles(String userid) throws Exception{
		return powerDao.queryUserRole(userid);
	}
	/**
	 *	激活卡申请  第一位
	 *	激活卡复核  第二位
	 *	激活卡核准  第三位
	 *	延期        第四位
	 *	退卡申请    第五位
	 *	退卡审批    第六位 
	 */
	@Override
	public boolean checkPower(String powers,String checkvalue) throws Exception{
		boolean flag=false;
		//激活卡申请
		if(checkvalue.equals(Constants.ACTIR_CARD_POWER_APPLY)){
			flag=check(powers.substring(0, 1));
		}
		//激活卡复核
		else if(checkvalue.equals(Constants.ACTIR_CARD_POWER_REVIEW)){
			flag=check(powers.substring(1, 2));
		}
		//激活卡核准
		else if(checkvalue.equals(Constants.ACTIR_CARD_POWER_APPROVAL)){
			flag=check(powers.substring(2, 3));
		}
		//延期
		else if(checkvalue.equals(Constants.ACTIR_CARD_POWER_DELAY)){
			flag=check(powers.substring(3, 4));
		}
		//退卡申请
		else if(checkvalue.equals(Constants.ACTIR_CARD_POWER_BACKAPPLY)){
			flag=check(powers.substring(4, 5));
		}
		//退卡审批
		else if(checkvalue.equals(Constants.ACTIR_CARD_POWER_BACKAPP)){
			flag=check(powers.substring(5, 6));
		}
		return flag;
	}
	/**
	 * 校验是否为1
	 * @param value
	 * @return
	 */
	private boolean check(String value){
		if("1".equals(value)){
			return true;
		}else{
			return false;
		}
	}
	
}
